import { Service } from 'egg';
import * as moment from 'moment';
import { MsgStatus } from '../../model/msg';

export default class AdminMsgService extends Service {
    public async getMsgByPage(adminId: number, page: number, pageSize: number) {
        const data = await this.service.db.msg.getByPage(adminId, page, pageSize);
        if (data) {
            data.forEach((item) => {
                item.data = moment(data).format('YYYY-MM-DD HH:mm:SS');
            });
        }
        const count = await this.service.db.msg.getCountByAdmin(adminId);
        return {
            page,
            data,
            count,
            pageSize,
        };
    }

    public async readMsg(msgId: number) {
        await this.service.db.msg.update(msgId, {
            status: MsgStatus.Read,
        });
    }

    public async deleteMsg(msgId: number) {
        await this.service.db.msg.delete(msgId);
    }
}
